<template>
  <div class="cover-upload">
    <el-upload
      name="file"
      :show-file-list="false"
      :multiple="false"
      :http-request="uploadImage"
      accept=".png,.jpg,.jpeg,JPG,.gif,.GIF,.bmp,.BMP,.PNG"
    >
      <div class="cover-upload-btn">
        <template v-if="localFile">
          <img :src="localFile" alt="" />
        </template>
        <template v-else>
          <img
            :src="
              (imageUrlPrefix ? imageUrlPrefix : proxy.globalInfo.imageUrl) +
              modelValue.imageUrl
            "
            alt=""
            v-if="modelValue && modelValue.imageUrl"
          />
          <span class="iconfont icon-add" v-else></span>
        </template>
      </div>
    </el-upload>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from "vue";
import { useRouter, useRoute } from "vue-router";
const { proxy } = getCurrentInstance();
const router = useRouter();
const route = useRoute();
const props = defineProps({
  imageUrlPrefix: {
    type: String,
  },
  modelValue: {
    type: Object,
    default: null,
  },
});
const emit = defineEmits(["update:modelValue"]);
//是否是本地图片
const localFile = ref();
const uploadImage = async (file) => {
  file = file.file;
  let img = new FileReader();
  img.readAsDataURL(file);
  img.onload = (e) => {
    localFile.value = e.target.result;
    emit("update:modelValue", file);
  };
};
</script>

<style lang="scss" scoped>
.cover-upload {
  .cover-upload-btn {
    width: 150px;
    height: 150px;
    background: #f5f5f5;
    display: flex;
    align-items: center;
    justify-content: center;
    .iconfont {
      font-size: 50px;
      color: #ddd;
    }
    img {
      width: 100%;
      object-fit: cover;
    }
    cursor: pointer;
  }
}
</style>
